package org.xqyy.eval.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DataFormatUtil {

	public static SimpleDateFormat datetimeFormat = new SimpleDateFormat(
			"yyyy-MM-dd hh:mm:ss");
	public static SimpleDateFormat dateFormat = new SimpleDateFormat(
			"yyyy-MM-dd");

    public static SimpleDateFormat defectDateFormat = new SimpleDateFormat(
            "yyyy-MM");

	public static Date parseStringToDate(String dateStr) throws ParseException {
		return dateFormat.parse(dateStr);
	}

	public static String formatStringToDate(Date date) throws ParseException {
		return datetimeFormat.format(date);
	}

	public static Date parseStringToDateStamp(String dateStr)
			throws ParseException {
		return datetimeFormat.parse(dateStr);
	}

	public static String formatStringToDateStamp(Date date)
			throws ParseException {
		return dateFormat.format(date);
	}

	public static int daysBetween(Date smdate, Date bdate)
			throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		smdate = sdf.parse(sdf.format(smdate));
		bdate = sdf.parse(sdf.format(bdate));
		Calendar cal = Calendar.getInstance();
		cal.setTime(smdate);
		long time2 = cal.getTimeInMillis();
		cal.setTime(bdate);
		long time1 = cal.getTimeInMillis();
		long between_days = (time2 - time1) / (1000 * 3600 * 24);
		return Integer.parseInt(String.valueOf(between_days));
	}

	public static int daysBetween(String smdate, String bdate)
			throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal = Calendar.getInstance();
		cal.setTime(sdf.parse(smdate));
		long time2 = cal.getTimeInMillis();
		cal.setTime(sdf.parse(bdate));
		long time1 = cal.getTimeInMillis();
		long between_days = (time2 - time1) / (1000 * 3600 * 24);

		return Integer.parseInt(String.valueOf(between_days));
	}

	public static int yearsBetween(Date smdate, Date bdate)
			throws ParseException {
		Calendar ca1 = Calendar.getInstance();
		ca1.setTime(smdate);
		Calendar ca2 = Calendar.getInstance();
		ca2.setTime(bdate);
		System.out.println(ca1.get(Calendar.YEAR));
		System.out.println(ca2.get(Calendar.YEAR));
		int year = ca2.get(Calendar.YEAR)-ca1.get(Calendar.YEAR);
		return year;
	}
	
	 
}
